<?php

/*
 * 仓库管理
 * @author wyb
 */

namespace Seller\Controller;

class OrderExpressController extends BaseController {

    private $_model = null;

    public function _initialize() {
        parent::_initialize();
        $this->_model = D('logistics_order');
    }

   public function lists(){
        $data['ad.need_id'] = $this->__get['id'];

         $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $count = $this->_model->alias('ad')->where($data)->count();
        //分页
        $pagesize = 20; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $limit = $offset . ',' . $pagesize;
        $list = $this->_model->alias('ad')
                    ->join('LEFT JOIN zt_logistics_supply as a ON a.id = ad.supply_id')
                    ->where($data)
                    ->field('ad.*,a.company_name,a.mobile')
                    ->select();
       $this->assign('ref',$this->__get);
        $this->assign('lists',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 地址添加
     */
    public function add() {
        if (IS_POST) {
            isset($this->__post['truename']) && $this->__post['truename'] ? '' : $this->error('信息错误');
            isset($this->__post['phone']) && $this->__post['phone'] ? '' : $this->error('信息错误');
            isset($this->__post['address']) && $this->__post['address'] ? '' : $this->error('信息错误');
             isset($this->__post['city_id']) && $this->__post['city_id'] ? '' : $this->error('信息错误');
            if ($this->_model->add(array('truename' => $this->__post['truename'],'lng' => $this->__post['lng'],'lat' => $this->__post['lat'],'phone'=>$this->__post['phone'],'city_id'=>$this->__post['city_id'], 'address' => $this->__post['address'], 'add_time' => time(), 'customer_id' => $this->__seller_info['id']))) {
                $this->success('地址添加成功',U('lists'));
            } else {
                $this->error('地址添加失败');
            }
        }
        $this->assign('address',M('area')->where(array('parentid' => 0))->select());
        $this->display('form');
    }

    /**
     * 地址修改
     */
    public function edit() {
        if (IS_POST) {
            isset($this->__post['id']) && intval($this->__post['id']) > 0 ? '' : $this->error('信息错误');
            isset($this->__post['truename']) && $this->__post['truename'] ? '' : $this->error('信息错误');
            isset($this->__post['phone']) && $this->__post['phone'] ? '' : $this->error('信息错误');
            isset($this->__post['address']) && $this->__post['address'] ? '' : $this->error('信息错误');
            isset($this->__post['city_id']) && $this->__post['city_id'] ? '' : $this->error('信息错误');
            if ($this->_model->save(array('id' => $this->__post['id'],'lng' => $this->__post['lng'],'lat' => $this->__post['lat'],'phone'=>$this->__post['phone'],'city_id'=>$this->__post['city_id'], 'truename' => $this->__post['truename'], 'address' => $this->__post['address'], 'add_time' => time(), 'customer_id' => $this->__seller_info['id']))) {
                $this->success('地址更新成功',U('lists'));
            } else {
                $this->error('地址更新失败');
            }
        }
        if (!isset($this->__get['id']) || intval($this->__get['id']) <= 0)
            exit('信息不存在');
        $info = $this->_model->where(array('id' => $this->__get['id'], 'customer_id' => $this->__seller_info['id']))->find();
        if (!$info)
            $this->error('信息不存在');
        $this->assign('address',M('area')->where(array('parentid' => 0))->select());
        $this->assign('info', $info);
        $this->display('form');
    }

    /**
     * 地址删除
     */
    public function del() {
        if (!isset($this->__get['id']) || intval($this->__get['id']) <= 0)
            exit('信息不存在');
        if( $this->_model->where(array('id' => $this->__get['id'], 'customer_id' => $this->__seller_info['id']))->delete() ) {
            $this->success('地址删除成功',U('lists'));
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 选定物流商
     */
    public function ischoice(){
        $need_info = M('logistics_needs')->find($this->__get['need_id']);
        if ($need_info['is_choice'] == 1) $this->error('已选定物流商，不可重新选择');
        if ($this->__get['id']){
            $data['is_choice'] = 1;
            $data['id'] = $this->__get['id'];
            if(M('logistics_order')->save($data)){
                $need_info['is_choice'] = 1;
                $need_info['express_order_id'] = $this->__get['id'];
                M('logistics_needs')->save($need_info);
                $this->success('选择成功');
            }else{
                $this->error('选择失败');
            }
        }
    }

}
